package nioTest.com.io;

import java.io.FileInputStream;
import java.nio.ByteBuffer;

/**
 * @author : yang.xp
 * @date : 2022/3/14 0014 17:49
 * @description : file channel 读案例，读写案例就不写了
 */
public class NIOFileChannel02 {

    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new FileInputStream("./log/test.log");
            ByteBuffer buffer = ByteBuffer.allocate(fileInputStream.available());

            // read 读到 -1， 0, 大于0 什么含义
            int read = fileInputStream.getChannel().read(buffer);
            System.out.println(read);

            fileInputStream.getChannel().position(0);
            ByteBuffer allocate = ByteBuffer.allocate(10);
            int read1 = fileInputStream.getChannel().read(allocate);
            System.out.println(read1);

            fileInputStream.getChannel().position(0);
            ByteBuffer allocate1 = ByteBuffer.allocate(5);
            int read2 = fileInputStream.getChannel().read(allocate1);
            System.out.println(read2);

            buffer.flip();
            allocate.flip();
            allocate1.flip();
//
//
//            byte[] bytes = new byte[1024];
//            int i = 0;
//            while (buffer.hasRemaining()) {
//                bytes[i] = buffer.get();
//                i++;
//            }
//            System.out.println(new String(bytes, 0, i));
            System.out.println(new String(buffer.array()));
            System.out.println(new String(allocate.array()));
            System.out.println(new String(allocate1.array()));
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}
